package com.example.bigwork.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;


import com.example.bigwork.R;
import com.example.bigwork.Shop;
import com.example.bigwork.Shopdetail;
import com.example.bigwork.adapter.PlanetGridAdapter;

import java.util.ArrayList;
import java.util.List;

public class RecommendFragment extends Fragment {

    //视图内容的展示
    //gridview展示页面
    private GridView gvShop;
    //商品数据源
    private List<Shop> shops;
    //声明商品适配器对象
    private PlanetGridAdapter shopAdapter;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        //绑定布局文件
        View recommendPage = inflater.inflate(R.layout.recomment_tablayout_activity,null);

        //获取GridView控件对象
        gvShop = (GridView) recommendPage.findViewById(R.id.gv_plant);


        //初始化商品数据源
        createShop();

        //设置点击事件监听器
        gvShop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //实现页面的跳转
                //1.实例化intent对象，同时设置跳转意图
                Intent shopdetail = new Intent();
                shopdetail.setClass(
                        getActivity(),
                        Shopdetail.class
                );
                //2.跳转的时候带着当前网格中的信息过去，根据position从shops中找到对应数据
                //并使用bubble进行封装
                Shop shop1 = shops.get(position);
                Bundle bundle = new Bundle();
                bundle.putSerializable("shop", shop1);

                //3. 将bundle对象添加到intent中
                shopdetail.putExtra("obj",bundle);

                //4.启动跳转
                startActivity(shopdetail);
            }
        });






        //实例化planeGridAdapter对象
        shopAdapter = new PlanetGridAdapter(
                getActivity(),
                R.layout.plant_tablayout_activity,
                shops
        );
        //给用于显示商品信息的gridview绑定适配器
        gvShop.setAdapter(shopAdapter);


        //绑定点击事件监听器




        return recommendPage;

    }



    //初始化商品源
    private void createShop() {

        shops = new ArrayList<>();
        //添加商品数据
        Shop fireworks = new Shop(
                R.drawable.fireworks,
                "包邮微瑕蜡烛花烛20厘米",
                "退货包运费 急速退款",
                "￥13"
        );
        shops.add(fireworks);
        Shop gril = new Shop(
                R.drawable.gril,
                "女童裤子 2022春新款,女童裤子 2022春新款",
                "退货包运费 满30反2退货包运费 满30反2退货包运费 满30反2退货包运费 满30反2退货包运费 满30反2",
                "￥14.88"
        );
        shops.add(gril);

        Shop mango = new Shop(
                R.drawable.mango,
                "正宗海南小台芒,女童裤子 2022春新款",
                "满21减3 坏了包赔",
                "￥18.7"
        );
        shops.add(mango);

        Shop lens = new Shop(
                R.drawable.lens,
                "一次性擦镜纸湿巾",
                "24小时发货",
                "￥1.7"
        );
        shops.add(lens);

        Shop lens1 = new Shop(
                R.drawable.lens,
                "一次性擦镜纸湿巾",
                "24小时发货",
                "￥1.7"
        );
        shops.add(lens1);


    }


}


